package com.zhiqianbao.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;

import com.zhiqianbao.R;

import java.io.BufferedReader;
import java.io.InputStreamReader;


public class PayActivity extends Activity implements View.OnClickListener{
    public static final String BUNDLE_PREPAYID = "prepay_id";
    public static final String BUNDLE_TOTALFEE = "total_fee";
    private String prepayId;
    private String totalFee;

    private WebView vWebView;
    private TextView vTotalFee;
    private TextView topbar_textview_title;
    private Button button_return;
    private Button button_cancel;
    private String totalfee ;
    private String term;
    private String cash_fee;
    private TextView shi_pay;
    private TextView curren_data;
    private String prepay_id;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay);
        iniViexw();
        intData();
        addListner();
        String jumpUrl = "";

    }

    private void intData() {
        Bundle zhifubao_pay = getIntent().getExtras().getBundle("zhifubao_pay");
        if (zhifubao_pay!=null){
            totalfee = zhifubao_pay.getString("totalfee");
            term = zhifubao_pay.getString("term");
            cash_fee = zhifubao_pay.getString("cash_fee");
            prepay_id = zhifubao_pay.getString("prepay_id");
            toH5Pay(prepay_id,"");
            shi_pay.setText(cash_fee);
            curren_data.setText(term);
            vTotalFee.setText(totalfee);
        }
    }

    private void addListner() {
        button_return.setOnClickListener(this);
        button_cancel.setOnClickListener(this);
    }

    private void iniViexw() {
        vWebView = (WebView) findViewById(R.id.pay_webView);
        vTotalFee = (TextView) findViewById(R.id.pay_totalFee);
        shi_pay = (TextView)findViewById(R.id.shi_pay);
        curren_data = (TextView) findViewById(R.id.curren_data);
        button_return = (Button) findViewById(R.id.button_return);
        button_cancel = (Button) findViewById(R.id.button_cancel);
        topbar_textview_title = (TextView) findViewById(R.id.topbar_textview_title);
        topbar_textview_title.setText("米米贷");

    }

    private void toH5Pay(String prepayId, String jumpUrl){
        WebSettings webSettings = vWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        CookieManager.getInstance().setAcceptCookie(true);
        vWebView.setBackgroundColor(0x00000000);
        vWebView.setLayerType(View.LAYER_TYPE_SOFTWARE,null);
        String alipayWapJsStr = getFromAssets("alipaywap.js");

        String doApiStr = " function doApi(){alipay_wap('" + prepayId + "','" + jumpUrl + "');}; ";
        String onBtnClickStr = " function onBtnClick(){doApi()}; ";
        String doClickBtnStr = " function doClickBtn(){var vBtn = document.getElementById('btn'); vBtn.click();}; ";
        String onLoadStr = " window.onload = function () { }; ";
        String htmlStr = "<html><head><script type=\"text/javascript\">" + alipayWapJsStr + onLoadStr + doApiStr + onBtnClickStr + doClickBtnStr + "</script></head><body><button id='btn' onclick='onBtnClick()' style='width:45%;height:100%;background:#ff0000;color:#ffffff'>确定</button></body></html>";
        vWebView.loadDataWithBaseURL("http://localhost/", htmlStr, "text/html", "utf-8", null);
    }

    public String getFromAssets(String fileName){
        String result="";
        try {
            InputStreamReader inputReader = new InputStreamReader( getResources().getAssets().open(fileName) );
            BufferedReader bufReader = new BufferedReader(inputReader);
            String line="";
            while((line = bufReader.readLine()) != null)
                result += line;
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


    @Override
    public void onClick(View v) {
        if(R.id.button_return==v.getId()) {
                finish();
        }
        else if(R.id.button_cancel==v.getId()){
                finish();
        }
    }
}
